package com.lyl.serviceucenter.controller.api;


import com.lyl.commonutils.DateUtils;
import com.lyl.commonutils.JwtUtils;
import com.lyl.commonutils.R;
import com.lyl.commonutils.redis.RedisConstants;
import com.lyl.commonutils.redis.RedisUtils;
import com.lyl.exception.ZxjyException;
import com.lyl.serviceucenter.entity.Member;
import com.lyl.serviceucenter.entity.vo.LoginVo;
import com.lyl.serviceucenter.entity.vo.RegisterVo;
import com.lyl.serviceucenter.service.MemberService;
import com.lyl.vo.order.UcenterMemberWebVoOrder;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-06-18
 */
@RestController
@RequestMapping("/serviceucenter/api/member")
public class MemberApiController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    private MemberService memberService;


    // 绑定微信id 与手机号码

    @PostMapping("bindPhone/{openId}/{phone}/{code}")
    public R bindPhone(@PathVariable(name = "openId") String openId,
                       @PathVariable(name = "phone") String phone,
                       @PathVariable(name = "code") String code) {


            //绑定手机号码顺便登录
        String    token =  memberService.bindPhone(openId,phone,code);



        // 登录量加1 登录信息在4号数据库
        RedisUtils.setDataBase(redisTemplate,4);
        // key为当前日期的登录量
        RedisUtils.incr(redisTemplate, DateUtils.formatDate(new Date(),0)+"/"+ RedisConstants.LOGIN_NUM,1);

        return R.ok().data("token", token);

    }


    // 支付的时候 获取用户信息
    //根据token字符串获取用户信息
    @PostMapping("getInfoUc/{id}")
    public UcenterMemberWebVoOrder getInfo(@PathVariable Long id) {
        //根据用户id获取用户信息
        Member ucenterMember = memberService.getById(id);
        UcenterMemberWebVoOrder memeber = new UcenterMemberWebVoOrder();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
    }


    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        String token =null;
        if(StringUtils.isEmpty(loginVo.getCode()))
               token = memberService.login(loginVo);
        else //验证码登录
            token= memberService.loginByCode(loginVo);


        // 登录量加1 登录信息在4号数据库
        RedisUtils.setDataBase(redisTemplate,4);
        // key为当前日期的登录量
        RedisUtils.incr(redisTemplate, DateUtils.formatDate(new Date(),0)+"/"+ RedisConstants.LOGIN_NUM,1);

        return R.ok().data("token", token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
      boolean isOk=  memberService.register(registerVo);
      if(isOk){

          return R.ok().message("注册成功");
      }

      return R.error().message("注册失败,稍后再试");
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request) {
        System.out.println("获取用户信息:token"+request.getHeader("token"));
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request,"id");
            Member member= memberService.getById(Long.parseLong(memberId));
            return R.ok().data("item", member);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ZxjyException(20001, "error");
        }
    }
}

